package com.shiro.distribution.redis;

import com.shiro.distribution.DistributeCacheRpository;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 功能或描述：redis cache dao
 *
 * Package com.shiro.distribution.redis
 * Author: DR.YangLong
 * Date: 14-9-30
 * Time: 上午10:13
 * Email: 410357434@163.com
 * Version: 1.0
 * Module: 修正:            日期：
 */
public class RedisRepository<K,V> implements DistributeCacheRpository<K,V>{
    private static final String REDIS_PREFIX_REPO="dr_redis_repo:";
    /**
     * redis spring template{@link org.springframework.data.redis.core.RedisTemplate}
     */
    private RedisTemplate<String,V> redisTemplate;
    /**
     * cache中key前缀
     */
    private String keyPrefix=REDIS_PREFIX_REPO;

    public RedisRepository() {
    }

    public RedisRepository(RedisTemplate<String, V> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public V get(K key) {
        return redisTemplate.opsForValue().get(generateKey(key));
    }

    @Override
    public V add(K key, V value) {
        String newKey=generateKey(key);
        V previous=redisTemplate.opsForValue().get(newKey);
        redisTemplate.opsForValue().set(newKey,value);
        return previous;
    }

    @Override
    public V delete(K key) {
        String newKey=generateKey(key);
        V previous=redisTemplate.opsForValue().get(newKey);
        redisTemplate.delete(newKey);
        return previous;
    }

    @Override
    public void clear() {
        Set<String> keySet = redisTemplate.keys(this.getKeyPrefix()+"*");
        redisTemplate.delete(keySet);
    }

    @Override
    public int getSize() {
        String newKey = this.getKeyPrefix() + "*";
        Set<String> keySet = redisTemplate.keys(newKey);
        if (keySet == null) {
            return 0;
        }
        return keySet.size();
    }

    @Override
    public Set<K> getKeys() {
        String newKey = this.getKeyPrefix() + "*";
        Set<String> keySet = redisTemplate.keys(newKey);
        Pattern pattern = Pattern.compile(this.getKeyPrefix() + "(.*)");
        Set<K> kSet = new HashSet<K>(keySet.size());
        for (String key : keySet) {
            Matcher matcher = pattern.matcher(key);
            if (matcher.find()) {
                K k = (K) matcher.group(1);
                kSet.add(k);
            }
        }
        return kSet;
    }

    @Override
    public Collection<V> getValues() {
        String newKey = this.getKeyPrefix() + "*";
        Set<String> keySet = redisTemplate.keys(newKey);
        List<V> valueList = null;
        if (keySet != null && keySet.size() > 0) {
            valueList = redisTemplate.opsForValue().multiGet(keySet);
        }
        return valueList;
    }

    @Override
    public String generateKey(Object key) {
        String byteString = (String)key;
        return this.getKeyPrefix()+byteString;
    }

    public RedisTemplate<String, V> getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate<String, V> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public String getKeyPrefix() {
        return keyPrefix;
    }

    public void setKeyPrefix(String keyPrefix) {
        this.keyPrefix = keyPrefix;
    }
}
